<?php
!defined ('P_W') && exit('Forbidden');

class PW_HouseInfo {

	var $_hdb;
	
	function PW_HouseInfo() {
		$this->__construct();
	}
	
	function __construct() {
		$this->_hdb = $this->_getHouseInfoDB();
	}

	function getPages($where, $page, $perpage, $url, $returnTotal=false) {
		$total = $this->_hdb->getCount($where);
		$pages = numofpage($total, $page, ceil($total/$perpage), $url);
		if ($returnTotal) return array($total, $pages);
		return $pages;
	}

	function getInfoById($id) {
		$info = $this->_hdb->getInfoById($id);
		return $info;
	}

	function getAllInfoById($id) {
		$info = $this->_hdb->getAllInfoById($id);
		return $info;
	}

	function getListByIds($ids) {
		return $ids ? $this->_hdb->getListByIds($ids) : array();
	}

	function getList($where, $offset = 0, $limit = 20, $order = 0) {
		$infodb = $this->_hdb->searchList($where, $offset, $limit, $order);
		foreach ($infodb as $key => $value) {
			$value['logo'] = $this->getLogo($value['logo']);
			$infodb[$key] = $value;
		}
		return $infodb;
	}

	function getLogo($logo) {
		$logo_img = '';
		if ($logo) {
			list($logo_img, $logo_type) = geturl($logo, 'lf');
		}
		!$logo_img && $logo_img = 'mode/house/images/non-house.gif';

		return $logo_img;
	}

	function checkFieldData($field, $value, $type) {
		if (empty($value) || !is_array($value)) {
			return false;
		}
		if ($type == 'multi') {
			$bitv = 0;
			foreach ($value as $k => $v) {
				$v > 0 && $bitv += 1 << (intval($v)-1);
			}
			return $this->_hdb->getByMultiField($field, $bitv);
		} else {
			return $this->_hdb->getBySingleField($field, $value);
		}
	}

	function updateByIds($pwSQL, $ids) {
		if ($ids && is_array($ids)) {
			$this->_hdb->updateByIds($pwSQL, $ids);
		}
	}
	function updateCollectnum($id) {
		$this->_hdb->updateCollectnum($id);
	}

	function deleteByIds($ids) {
		if (!$ids || !is_array($ids)) {
			return false;
		}
		$arr = $this->getListByIds($ids);
		foreach ($arr as $key => $value) {
			$value['logo'] && pwDelatt($value['logo']);
		}
		$this->_hdb->deleteByIds($ids);
	}

	function getDeveloperById($id) {
		if (!$id) return array();
		$temp = House::loadDB('Developer');
		if ($info = $temp->getById($id)) {
			$info['introduction'] = str_replace("\n", '<br />', $info['introduction']);
		}
		return $info;
	}

	function getDeveloper() {
		$temp = House::loadDB('Developer');
		return $temp->getList('', 0, null);
	}

	function paramCheck($data) {
		$arr = array();
		foreach ($data as $key => $value) {
			if ($value) {
				$arr[$key] = $value;
			}
		}
		return $arr;
	}

	function urlCompound($url, $url_add, $except = '') {
		foreach ($url_add as $key => $value) {
			if ($key == $except) continue;
			if (is_array($value)) {
				foreach ($value as $k => $v) {
					!is_array($v) && $url .= "&$key[$k]=" . rawurlencode($v);
				}
			} else {
				$url .= "&$key=" . rawurlencode($value);
			}
		}
		return $url;
	}

	function urlCompounds($url, $url_add, $except) {
		foreach ($url_add as $key => $value) {
			if (in_array($key, $except)) continue;
			if (is_array($value)) {
				foreach ($value as $k => $v) {
					!is_array($v) && $url .= "&$key[$k]=" . rawurlencode($v);
				}
			} else {
				$url .= "&$key=" . rawurlencode($value);
			}
		}
		return $url;
	}
	
	function getBitValue($bit, $vkey, $implode = '') {
		if (!$bit) return '';
		global $house_field;
		$retv = array();
		foreach ($house_field[$vkey] as $key => $value) {
			if ($bit & (1 << ($key-1))) {
				$retv[] = $value['name'];
			}
		}
		return $implode ? implode($retv, $implode) : $retv;
	}
	
	function _getHouseInfoDB() {
		return House::loadDB('HouseInfo');
	}
}
?>